<template>
  <div class="user-profile">
    <div class="avatar-container">
      <img :src="user.avatar" alt="User Avatar" class="avatar">
      <div class="online-status" :class="{ 'online': user.isOnline }"></div>
    </div>

    <h2 class="username">{{ user.username }}</h2>
<!--    <p class="user-level">等级 {{ user.level }}</p>-->

    <div class="stats">
      <div class="stat-item">
        <span class="stat-value">{{ user.playCount }}</span>
        <span class="stat-label">游玩场次</span>
      </div>
      <div class="stat-item">
        <span class="stat-value">{{ user.rating }}</span>
        <span class="stat-label">分数</span>
      </div>
      <div class="stat-item">
        <span class="stat-value">{{ user.winRate }}%</span>
        <span class="stat-label">胜率</span>
      </div>
    </div>

    <div class="progress-container">
<!--      <div class="progress-bar" :style="{ width: user.expProgress + '%' }"></div>-->
<!--      <span class="progress-text">经验: {{ user.currentExp }}/{{ user.nextLevelExp }}</span>-->
    </div>

    <button class="edit-profile-btn">
      <i class="fas fa-cog"></i> 编辑资料
    </button>
    <button class="edit-profile-btn" @click="logout">
      <i class="fas fa-cog"></i> 退出登录
    </button>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import { useUser } from '../composables/useUser'
import {jwtDecode} from "jwt-decode";
import router from "@/routers/index.js";

const { user,getUserProfile } = useUser()

const logout = () => {
  localStorage.removeItem('token')
  setTimeout(() => {
    window.location.href = '/login'
  }, 500) // 延迟重定向以确保token已被删除
}

onMounted(() => {
  const username = jwtDecode(localStorage.getItem('token')).sub
  getUserProfile(username)
})
</script>

<style scoped>
.user-profile {
  padding: 20px;
  color: white;
  text-align: center;
}

.avatar-container {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 15px;
}

.avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #4cc9f0;
}

.online-status {
  position: absolute;
  bottom: 10px;
  right: 10px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: #ff5a5f;
  border: 2px solid #1a1a2e;
}

.online-status.online {
  background-color: #4cc9f0;
}

.username {
  font-size: 1.5rem;
  margin: 0 0 5px;
  color: #ffffff;
}

.user-level {
  font-size: 1rem;
  color: #a5a5a5;
  margin: 0 0 20px;
}

.stats {
  display: flex;
  justify-content: space-around;
  margin: 20px 0;
}

.stat-item {
  display: flex;
  flex-direction: column;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: bold;
  color: #4cc9f0;
}

.stat-label {
  font-size: 0.8rem;
  color: #a5a5a5;
}

.progress-container {
  background: rgba(255, 255, 255, 0.1);
  height: 20px;
  border-radius: 10px;
  margin: 20px 0;
  position: relative;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #4cc9f0 0%, #4361ee 100%);
  border-radius: 10px;
  transition: width 0.5s ease;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.7rem;
  color: white;
  z-index: 1;
}

.edit-profile-btn {
  background: rgba(76, 201, 240, 0.2);
  color: #4cc9f0;
  border: 1px solid #4cc9f0;
  border-radius: 5px;
  padding: 8px 15px;
  margin: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.edit-profile-btn:hover {
  background: rgba(76, 201, 240, 0.4);
}
</style>
